package view.panel
{
	import control.panel.IPanelControl;
	
	import core.Control;
	import core.View;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import global.GameState;
	import global.Resource;
	
	import utils.ColorTool;
	import utils.DisplayTool;
	import utils.animate.TweenPanelOpen;
	
	import view.components.LabelButton;
	import view.components.TempBitmap;

	public class AbsPanel extends Sprite implements IPanel
	{
		public var bgWidth:int = 300;
		public var bgHeight:int = 200;
		protected var _id:int;
		public var defaultX:int;
		public var defaultY:int;
		protected var closeBut:LabelButton;
		protected var _bgAlpha:Number;
		
		protected var _view:View = View.getInstance();
		protected var panelControl:IPanelControl
		protected var tweenPanelOpen:TweenPanelOpen;
		protected var title:DisplayObject;
		
		protected var isAlert:Boolean;
	
		
		public function AbsPanel(panelControl:IPanelControl)
		{
			this.panelControl = panelControl;
		}
		
		public function get id():int
		{
			return _id;
		}
		
		protected function initTitle(titleKey:String):void
		{
			title = Resource.getImg(titleKey);
			title.x = 20;
			title.y = - title.height * .5;
			
			if (title is TempBitmap)
			{
				title.addEventListener(Event.COMPLETE,onTitleComplete);
			}
			
			addChild(title);
		}
		
		private function onTitleComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onTitleComplete);
			title.y = - title.height * .5;
		}
		
		protected function resetBg(bgWidth:int,bgHeight:int,alpha:Number = 1,isCenter:Boolean = false,bgTopColor:uint = 0x666666,bgBottomColor:uint = 0x111111):void
		{
			_bgAlpha = alpha;
			this.bgHeight = bgHeight;
			this.bgWidth = bgWidth;
			initBg(bgTopColor,bgBottomColor);
			
			if (isCenter)
			{
				setCenter();
			}
		}
		
		protected function dropShadow():void
		{
			ColorTool.dropShadow(this);
		}
		
		public function setCenter():void
		{
			defaultX = (GameState.GAME_WIDTH - bgWidth) * .5;
			defaultY = (GameState.GAME_HEIGHT - bgHeight) * .5;
		}
		
		public function setTableCenter():void
		{
			defaultX = (GameState.GAME_WIDTH - bgWidth) * .5;
			defaultY = (GameState.GAME_TABLE_HEIGHT - bgHeight) * .5;
		}
		
		protected function initTweenPanelOpen():void
		{
			tweenPanelOpen = new TweenPanelOpen();
			
		}

		protected function initBg(bgTopColor:uint = 0x666666,bgBottomColor:uint = 0x111111):void
		{
			ColorTool.drawPanelBg(this.graphics,bgWidth,bgHeight,_bgAlpha,bgTopColor,bgBottomColor);
			/*var fillType:String = GradientType.LINEAR;
		    var colors:Array = [0xB7B7B7, 0x333333];
			var alphas:Array = [_bgAlpha, _bgAlpha];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(bgWidth, bgHeight, Math.PI / 2, 0, 0);
     
			with (this.graphics)
			{
				clear();
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(0,0,bgWidth,bgHeight,5,5);
				endFill();
				colors = [0x333333,0];
				matr.createGradientBox(bgWidth - 6,bgHeight - 6,Math.PI / 2,0,0);
				beginGradientFill(fillType,colors,alphas,ratios,matr);
				drawRoundRect(3,3,bgWidth - 6, bgHeight - 6,5,5);
				endFill();
			}*/
		}
		
		protected function initCloseBut():void
		{
			closeBut = new LabelButton(0,0,0,onClose,"panel_close");
		}
		
		protected function resetCloseBut():void
		{
			closeBut.x = bgWidth - 30;
			closeBut.y = 5;
			addChild(closeBut);
		}
		
		private function onClose(event:MouseEvent):void
		{
			close();
		}
		
		public function get self():DisplayObjectContainer
		{
			return this;
		}
		
		public function open():void
		{
			
			this.x = defaultX;
			this.y = defaultY;
			
			if (isAlert)
			{
				_view.hintLayer.addChild(this);
			}
			else
			{
				_view.panelLayer.addChild(this);
			}
			
			
			if (tweenPanelOpen)
			{
				tweenPanelOpen.setTarget(this);
				tweenPanelOpen.start();
			}
			
			Control.getInstance().sceneControl.addOpenedPanel(this);
		}
		
		public function close():void
		{
			DisplayTool.removeChild(this.parent,this);
			if (tweenPanelOpen)
			{
				tweenPanelOpen.stop();
			}
			Control.getInstance().sceneControl.removeOpenedPanel(this);
		}
	
	}
}